#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>

void random(int *num1, char *op, int *num2);
int judge(int num1, char op, int num2, double answer);

int main()
{
    int num1, num2, answer;
    char op;
    double result;
    int score = 0, wrongs = 0;

    srand(time(NULL));

    for (int i = 1; i <= 10; i++)
    {
        random(&num1, &op, &num2);
        printf("Question: %d %c %d\n", num1, op, num2);
        result = judge(num1, op, num2, answer);
        printf("Please input answer:");
        scanf("%.2lf",&answer);
        if (result == answer)
        {
            printf("Right!\n");
            score++;
        }
        else
        {
            printf("Not correct!\n");
            wrongs++;
        }
    }
    printf("Your score is %d.\nYou have %d wrongs.\n", score * 10, wrongs);
    return 0;
}

void random(int *num1, char *op, int *num2)
{
    *num1 = rand() % 10 + 1;
    *num2 = rand() % 10 + 1;
    int c = rand() % 4;
    switch (c)
    {
    case 0:
        *op = '+';
        break;
    case 1:
        *op = '-';
        break;
    case 2:
        *op = '*';
        break;
    case 3:
        *op = '/';
        break;
    }
}

int judge(int num1, char op, int num2, double answer)
{
    double result;
    switch (op)
    {
    case '+':
        result = num1 + num2;
        break;
    case '-':
        result = num1 - num2;
        break;
    case '*':
        result = num1 * num2;
        break;
    case '/':
        result = (double)num1 / num2;
        break;
    }
    if (fabs(result - answer) < 1e-6)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}
